﻿#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include <QStringList>
#include <QIODevice>
#include <users.h>
#include "fileio.h"
#include <QDebug>

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("登录界面");
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::on_btn_login_clicked()
{
    QString username = ui->lineEdit_name->text();
    QString password = ui->lineEdit_password->text();

    if(username == NULL && password == NULL)
    {
        QMessageBox::warning(this, "警告提示", "输入信息不能为空！！！");
        return;
    }

    IO io;
    QVector<User> v(io.IO::getFileUsernamePassword());

    if(v.isEmpty())
    {
        QMessageBox::warning(this, "警告提示", "数据库中无数据，请先注册用户");
        return;
    }

    for(int i = 0; i < v.size() ; i++)
    {
        if(username == v.at(i).username && password == v.at(i).password)
        {
            //登录成功
            this->hide();
            emit goMainWidget();
            return;
        }
        else
        {
            QMessageBox::critical(this,"错误提示","账号或密码错误，请重新输入");
        }
    }
}

void LoginWidget::on_btn_register_clicked()
{
    clearAllEditline();
    this->hide();
    emit goRegisterWidget();
}

void LoginWidget::on_btn_clear_clicked()
{
    clearAllEditline();
}

void LoginWidget::on_btn_exit_clicked()
{
    this->close();
}

void LoginWidget::clearAllEditline()
{
    ui->lineEdit_name->clear();
    ui->lineEdit_password->clear();
}


